package org.cp.dao.user.impl;

import java.util.List;

import org.cp.dao.BaseDAO;
import org.cp.dao.user.ITestUserDAO;
import org.cp.vo.Testuser;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class TestUserDAO extends BaseDAO implements ITestUserDAO {

	public void changeUserInfo(Testuser testUser) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(testUser);
		tx.commit();
		session.close();

	}

	public Testuser login(String accounts, String passWord) {
		Session session = getSession();
		String hql = "from Testuser where accounts=? and password=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,accounts);
		query.setParameter(1,passWord);
		List users = query.list();
		if(users.size() != 0){
			
			Testuser testUser = (Testuser)users.get(0);
			session.close();
			return testUser;
		}
		session.close();
		return null;
	}

	public void logout() {
		// TODO Auto-generated method stub

	}

	public void register(Testuser testUser) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(testUser);
		tx.commit();
		session.close();
	}
	public Testuser searchTestUserbyaccounts(String accounts) {
		Session session = getSession();
		String hql = "from Testuser where accounts=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,accounts);
		List users = query.list();
		if(users.size() != 0){
			Testuser testUser = (Testuser)users.get(0);
			session.close();
			return testUser;
		}
		session.close();
		return null;
	}


}
